import { defineStore } from "pinia";
import { useStorage } from "@vueuse/core";
import { useStoragePrefix } from "@/hooks";
declare type UserType  = {
  id: number
  name: string
  roleName: string
  [index:string]:any
} | Record<string, any>
declare type SettingType  = {
  layout:{
    asideWidth: number
    footerHeight: number
    headerHeight: number
    pattern: string
    collapsible: boolean
  }
  [index:string]:any
} | Record<string, any>
interface AppInterface {
  user: UserType
  tab: Record<string, any>
  setting: SettingType
}
const userKey = useStoragePrefix("USER")
const tabKey = useStoragePrefix("TAb")
const settingKey = useStoragePrefix("SETTING")
export const useAppStore = defineStore("app", {
  state: (): AppInterface => {
    return {
      user: useStorage(userKey, {
        id: 1,
        name: "admin",
        roleName: "超级管理员"
      }),
      tab: useStorage(tabKey, {

      }),
      setting: useStorage(settingKey, Object.assign({
        layout:{
          asideWidth: 230,
          footerHeight: 60,
          headerHeight: 60,
          pattern: "default",
          collapsible: false,
        }
      },window.appConfig.setting)),
    };
  },
  actions: {
    setUser(user: UserType) {
      this.user = user;
      return user;
    },
    setLayout(pattern: string){
      this.changeLayout("pattern", pattern)
    },
    changeLayout(key:string, value: number|string){
      this.setting.layout[key] = value
    },
    getSetting(){
      return this.setting;
    }
  },
});
